﻿using DMC.Core.Models.Dto;
using DMC.Core.Models.Queries;
using DMC.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Utility;
using MiniFox.Web.Filters;
using MiniFox.Web.Infrastructure;

namespace DMC.Web.Controllers
{
    [NoCache, ValidationFilter, Area(GlobalConstants.AREA_NAME)]
    public class ConnectionController : MvcControllerBase
    {
        [AutoWired]
        protected IConnectionService Component { get; set; }
        [HttpPost]
        public Response<IEnumerable<ConnectionDto>> Query([FromBody] QueryObject<ConnectionQuery> request)
        {
            var result = this.Component.Query(request);
            
            return result.Result.CreateResponse();
        }

        [HttpPost]
        public Response Create([FromBody] ConnectionDto request)
        {
            var result = this.Component.Create(request);
            
            return result.CreateResponse();
        }

        [HttpPost]
        public Response Update([FromBody] ConnectionDto request)
        {
            var result = this.Component.Update(request);
            
            return result.CreateResponse();
        }

        [HttpPost]
        public Response Delete([FromBody] ConnectionDto request)
        {
            this.Component.Delete(request);
            
            return new();
        }

        [HttpGet, AllowAnonymous]
        public Response<object[]> DbProviders()
        {
            var result = this.Component.GetDbProviders();
            
            return result.Result.CreateResponse();
        }

        [HttpPost]
        public Response Test([FromBody] ConnectionTestDto request)
        {
            Response response = new();

            this.Component.Test(request);
            return response;
        }

        [HttpGet]
        public Response Sync()
        {
            this.Component.Sync();
            
            return new();
        }
    }
}
